package common.BaseView
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.lz.football.module.base.IModuleView;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * _isDrag   是否需要拖动 
	 * _isCloseBtn 是否需要关闭按钮
	 * _isTurnTop 是否需要点击后弹到早上层  
	 * true可以，false不可以
	 * @author Administrator
	 * 
	 */	
	public class BaseView extends Sprite implements IModuleView
	{
		private var _isDrag:Boolean;
		private var _isCloseBtn:Boolean;
		private var _isTurnTop:Boolean;
		
		/**
		 *背景 
		 */
		public var _windowBg:MovieClip;
		/**
		 *关闭按钮 
		 */
		protected var _closeBtn:SimpleButton;
		/**
		 * 
		 */
		public var symbolSkin:MovieClip;
		
		/**
		 *面板宽 
		 */
		public var _viewWidth:int;
		/**
		 *面板高 
		 */
		public var _viewHeight:int;
		
		public function BaseView()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			_viewWidth = this.width;
			_viewHeight = this.height;
//			isDrag = true;
//			isCloseBtn = true;
			onResizeHandler();
			addEvtListener();
		}
		
		public function addEvtListener():void
		{
			GlobalDef.GameStage.addEventListener(Event.RESIZE,onResizeHandler);
			
		}
		
		
		public function onResizeHandler(event:Event = null):void
		{
			this.x = (GlobalDef.StageWidth - _viewWidth)>> 1;
			this.y = (GlobalDef.StageHeight - _viewHeight)>> 1;
		}		
		
		public function removeEvtListener():void
		{
			
			if(_closeBtn != null)_closeBtn.removeEventListener(MouseEvent.CLICK,onCloseHandler);
			GlobalDef.GameStage.removeEventListener(Event.RESIZE,onResizeHandler);
			if(_windowBg == null)return;
			if(_windowBg.stage)
			{
				_windowBg.stage.removeEventListener(MouseEvent.MOUSE_UP,onDragMouseUp);
			}
			_windowBg.removeEventListener(MouseEvent.MOUSE_DOWN,onDragMouseDown);
			_windowBg.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveToStage);
			_windowBg.removeEventListener(Event.ADDED_TO_STAGE,onAddToStage);
			
		}
		/**
		 * 销毁视图 
		 * 
		 */
		public function destroyView():void
		{
			_isTurnTop = false;
			_isCloseBtn = false;
			_isDrag = false;
			if(_closeBtn != null && _closeBtn.parent)
				_closeBtn.parent.removeChild(_closeBtn);
			_closeBtn = null;
			if(this.parent)
				this.parent.removeChild(this);
			symbolSkin = null;
			_windowBg = null;
			
		}
		
		public function showType($obj:Object):void
		{
		}
		
		
		
		
		/**
		 *是否可以拖动 
		 */
		public function get isDrag():Boolean
		{
			return _isDrag;
		}

		/**
		 * @private
		 */
		public function set isDrag(value:Boolean):void
		{
			_isDrag = value;
			if(_windowBg == null)return;
			if(_isDrag)
			{
				_windowBg.mouseChildren = true;
				_windowBg.mouseEnabled = true;
				_windowBg.addEventListener(MouseEvent.MOUSE_DOWN,onDragMouseDown);
				_windowBg.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveToStage);
				_windowBg.addEventListener(Event.ADDED_TO_STAGE,onAddToStage);
			}else
			{
				_windowBg.mouseChildren = false;
				_windowBg.mouseEnabled = false;
				_windowBg.removeEventListener(MouseEvent.MOUSE_DOWN,onDragMouseDown);
				_windowBg.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveToStage);
				_windowBg.removeEventListener(Event.ADDED_TO_STAGE,onAddToStage);
				if(_windowBg.stage)
				{
					_windowBg.stage.removeEventListener(MouseEvent.MOUSE_UP,onDragMouseUp);
				}
				
			}
		}
		/**
		 * 从移除舞台 
		 * @param event
		 * 
		 */		
		protected function onRemoveToStage(event:Event):void
		{
			if(_windowBg.stage)
			{
				_windowBg.stage.removeEventListener(MouseEvent.MOUSE_UP,onDragMouseUp);
			}
		}
		/**
		 * 添加到舞台 
		 * @param e
		 * 
		 */
		private function onAddToStage(e :Event):void
		{
			if(_windowBg.stage)
			{
				_windowBg.stage.addEventListener(MouseEvent.MOUSE_UP,onDragMouseUp);
			}
		}
		/**
		 * 点击拖动并显示最上层 
		 * @param event
		 * 
		 */
		protected function onDragMouseDown(event:MouseEvent):void
		{
			if(_isTurnTop)
				this.parent.setChildIndex(this,this.parent.numChildren - 1);
			if(this && this.parent && _isDrag)
			{
				this.parent.setChildIndex(this,this.parent.numChildren - 1);
				var rect :Rectangle = new Rectangle(0,0,GlobalDef.GameStage.stageWidth - _viewWidth,GlobalDef.GameStage.stageHeight - _viewHeight);
				this.startDrag(false,rect);
			}
		}
		/**
		 *停止拖动 
		 * @param e
		 * 
		 */
		protected function onDragMouseUp(e:MouseEvent):void
		{
			this.stopDrag();
		}
	
		
		/**
		 *是否有关闭按钮  
		 */
		public function get isCloseBtn():Boolean
		{
			return _isCloseBtn;
		}

		/**
		 * @private
		 */
		public function set isCloseBtn(value:Boolean):void
		{
			_isCloseBtn = value;
			if(_isCloseBtn)
			{
				_closeBtn = Reflection.createInstance("com.lz.commonUI.closeBtn");
				this.addChild(_closeBtn);
				_closeBtn.addEventListener(MouseEvent.CLICK,onCloseHandler);
				_closeBtn.x = (this.width - _closeBtn.width - 20);
				_closeBtn.y = 5;
			}
		}
		/**
		 *按钮 
		 * @param $clostbtn
		 * 
		 */
		public function setCloseBtn($clostbtn:* = null):void
		{
			if($clostbtn == null)
			{
				_closeBtn = Reflection.createInstance("com.lz.commonUI.closeBtn");
				this.addChild(_closeBtn);
				_closeBtn.x = (this.width - _closeBtn.width - 20);
				_closeBtn.y = 5;
			}
			else
			{
				_closeBtn = $clostbtn;
			}
			_closeBtn.addEventListener(MouseEvent.CLICK,onCloseHandler);
		}
		public function moveClose($x:int,$y:int):void
		{
			_closeBtn.x = $x;
			_closeBtn.y = $y;
		}
		/**
		 * 关闭面板 
		 * @param event
		 * 
		 */
		public function onCloseHandler(event:MouseEvent):void
		{
			if(this.parent)
				this.parent.removeChild(this);
		}
		
		/**
		 *点击到是否到最上层 
		 */
		public function get isTurnTop():Boolean
		{
			return _isTurnTop;
		}

		/**
		 * @private
		 */
		public function set isTurnTop(value:Boolean):void
		{
			_isTurnTop = value;
			
			if(_isTurnTop)
			{
				_windowBg.addEventListener(MouseEvent.MOUSE_DOWN,onDragMouseDown);	
			}else
			{
				_windowBg.removeEventListener(MouseEvent.MOUSE_DOWN,onDragMouseDown);
			}
		}


	}
}